package com.uniquindio.proyectomoviles.contentsproviders;
import java.util.ArrayList;
import java.util.List;

import com.uniquindio.proyectomoviles.entidades.Usuario;
 
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
 
public class DatabaseHandler extends SQLiteOpenHelper {
 
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;
 
    // Database Name
    private static final String DATABASE_NAME = "usuariosManager";
 
    // Contacts table name
    private static final String TABLE_CONTACTS = "usuario";
 
    // Contacts Table Columns names
    private static final String KEY_CEDULA = "cedula";
    private static final String KEY_EMAIL = "email";
    private static final String KEY_PASS = "password";
 
    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
 
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_CEDULA + " TEXT PRIMARY KEY," + KEY_EMAIL + " TEXT,"
                + KEY_PASS + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }
 
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
 
        // Create tables again
        onCreate(db);
    }
 
    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */
 
    // Adding new contact
    public void addContact(Usuario usuario) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(KEY_CEDULA, usuario.get_cedula()); // Contact Name
        values.put(KEY_EMAIL, usuario.get_email()); // Contact Name
        values.put(KEY_PASS, usuario.get_password()); // Contact Phone
 
        // Inserting Row
        db.insert(TABLE_CONTACTS, null, values);
        db.close(); // Closing database connection
    }
 
    // Getting single contact
    Usuario getContact(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
 
        Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_CEDULA,
                KEY_EMAIL, KEY_PASS }, KEY_CEDULA + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
 
        Usuario usuario = new Usuario(cursor.getString(0),
                cursor.getString(1), cursor.getString(2));
        // return contact
        return usuario;
    }
 
    // Getting All Contacts
    public List<Usuario> getAllContacts() {
        List<Usuario> usuarios = new ArrayList<Usuario>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;
 
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
 
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Usuario contact = new Usuario();
                contact.set_cedula(cursor.getString(0));
                contact.set_email(cursor.getString(1));
                contact.set_password(cursor.getString(2));
                // Adding contact to list
                usuarios.add(contact);
            } while (cursor.moveToNext());
        }
 
        // return contact list
        return usuarios;
    }
 
    // Updating single contact
    public int updateContact(Usuario usuario) {
        SQLiteDatabase db = this.getWritableDatabase();
 
        ContentValues values = new ContentValues();
        values.put(KEY_CEDULA, usuario.get_cedula());
        values.put(KEY_EMAIL, usuario.get_email());
        values.put(KEY_PASS, usuario.get_password());
 
        // updating row
        return db.update(TABLE_CONTACTS, values, KEY_CEDULA + " = ?",
                new String[] { String.valueOf(usuario.get_cedula()) });
    }
 
    // Deleting single contact
    public void deleteContact(Usuario usuario) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_CEDULA + " = ?",
                new String[] { String.valueOf(usuario.get_cedula()) });
        db.close();
    }
 
    // Getting contacts Count
    public int getContactsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_CONTACTS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();
 
        // return count
        return cursor.getCount();
    }
 
}